﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace carXapp2.MiscPages
{
    public partial class AddEditMaint : PhoneApplicationPage
    {
        private int carID=-1;
        private int maintID=-1;

        public AddEditMaint()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel.SingleMaintRecord;
        }

        private void LoadData()
        {
            
            //App.ViewModel.SingleMaintRecord.Date = DateTime.Now;
            
        }

        private void menuSave_click(object sender, EventArgs e)
        {
            bool success = false;
            if (maintID == -1)
            {
                success = addRecord();
            }
            else
            {
                success = UpdateRecord();
            }

            if (NavigationService.CanGoBack && success)
            {
                NavigationService.GoBack();
            }
        }

        private bool addRecord()
        {
            float lch, pco,tco;
            int miles;
            var rs1 = float.TryParse(txtLaborCharges.Text, out lch);
            var rs2 = float.TryParse(txtPartCost.Text, out pco);
            var rs3 = float.TryParse(txtTotalCost.Text, out tco);
            var rs4 = int.TryParse(txtMiles.Text, out miles);
            if (rs1 && rs2 && rs3 && rs4)
            {
                App.ViewModel.SingleMaintRecord.DateStr = txtDate.Value.Value.ToShortDateString();
                App.ViewModel.AddMaint(App.ViewModel.SingleMaintRecord);
                return true;

            }
            else
            {
                MessageBox.Show("Incorrect or Incomplete data");
                return false;
            }
            
        }

        private bool UpdateRecord()
        {
            float lch, pco,tco;
            int miles;
            var rs1 = float.TryParse(txtLaborCharges.Text, out lch);
            var rs2 = float.TryParse(txtPartCost.Text, out pco);
            var rs3 = float.TryParse(txtTotalCost.Text, out tco);
            var rs4 = int.TryParse(txtMiles.Text, out miles);
            if (rs1 && rs2 && rs3 && rs4)
            {
                App.ViewModel.SingleMaintRecord.DateStr = txtDate.Value.Value.ToShortDateString();
                App.ViewModel.SaveChangesToDB();
                return true;
            }
            else
            {
                MessageBox.Show("Incorrect or Incomplete data");
                return false;
            }

 
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string cid,mid;
            NavigationContext.QueryString.TryGetValue("carID", out cid);
            NavigationContext.QueryString.TryGetValue("maintID", out mid);
            int.TryParse(cid, out carID);
            if (mid != null)
            {
                int.TryParse(mid, out maintID);
            }
            LoadData();
                   
            base.OnNavigatedTo(e);
        }
    }
}